<?php
/**
 * 消息管理
 */
namespace app\modules\user\controllers;

use app\modules\user\models\News;
use app\modules\user\models\User;
use yii;
use app\libs\ApiControl;
use app\libs\Method;

class NewsController extends ApiControl {
    public $enableCsrfValidation = false;
    public $layout = 'user';

    /**
     * [actionIndex 系统消息列表]
     * @Author don
     * @Date   2021-09-28
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $page = Yii::$app->request->get('page',1);
        $beginTime = strtotime(Yii::$app->request->get('beginTime',''));
        $endTime = strtotime(Yii::$app->request->get('endTime',''));
        $sendId = Yii::$app->request->get('sendId','');
        $uid = Yii::$app->request->get('uid','');
        $id  = Yii::$app->request->get('id','');
        $where = "n.type=1";
        if ($beginTime) {
            $where .= " AND n.createTime>=$beginTime";
        }
        if ($endTime) {
            $where .= " AND n.createTime<=$endTime";
        }
        if ($id) {
            $where .= " AND n.id = $id";
        }
        if ($sendId) {
            $where .= " AND n.sendId = $sendId";
        }
        if ($uid) {
            $where .= " AND n.uid = $uid";
        }
        $model = new News();
        $data = $model->getAllNews($where, 20, $page);

        $page = Method::getPagedRows(['count'=>$data['count'],'pageSize'=>20, 'rows'=>'models']);
        return $this->render('news',['page' => $page,'data' => $data['data']]);
    }

    /**
     * [actionAdd 添加系统消息]
     * @Author don
     * @Date   2021-09-28
     * @return [type]     [description]
     */
    public function actionAdd(){
        if($_POST){
            $uid = Yii::$app->request->post('uid');
            $beginTime = strtotime(Yii::$app->request->post('beginTime'));
            $endTime = strtotime(Yii::$app->request->post('endTime'));
            $news = Yii::$app->request->post('news','');
            $adminId = Yii::$app->session->get('adminId');
            if (!$news) {
                die('<script>alert("信息内容不能为空");history.go(-1);</script>');
            }
            $where="1=1";
            if ($beginTime) {
                $where .= " AND createTime>=$beginTime";
            }
            if ($endTime) {
                $where .= " AND createTime<=$endTime";
            }
            if ($uid) {
                $where .= " AND uid = $uid";
            }

            $users = User::find()->where($where)->all();

            foreach($users as $v){
                $model = new News();
                $model->news = $news;
                $model->uid = $v['uid'];
                $model->status = 1;
                $model->type = 1;
                $model->createTime = time();
                $model->sendId = $adminId;
                $model->save();
            }
            $this->redirect("/user/news/index");
        }else{
            return $this->render('add');
        }
    }

    /**
     * [actionDelete 删除]
     * @Author don
     * @Date   2021-09-28
     * @return [type]     [description]
     */
    public function actionDelete(){
        $id = Yii::$app->request->get('id');
        $url = $_GET['url'];
        News::findOne($id)->delete();
        $this->redirect($url);
    }

}